package com.vcredit.creditcat.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.enumtype.AbilityEnum;
import com.vcredit.creditcat.model.CustAbility;
import com.vcredit.creditcat.persistence.CustAbilityExtMapper;
import com.vcredit.creditcat.persistence.base.CustAbilityMapper;
import com.vcredit.creditcat.service.product.CustLoanProductService;
import com.vcredit.creditcat.util.ConcurrentDateUtil;

@Service
public class CustAbilityService {
	@Autowired
	private CustAbilityMapper mapper;

	@Autowired
	private CustAbilityExtMapper extMapper;
	
	@Autowired
	private CustRecommendCardService custRecommendCardService;
			
	@Autowired
	private CustLoanProductService custLoanProductService;

	public void insert(CustAbility obj) {
		mapper.insert(obj);
	}

	public void update(CustAbility obj) {
		mapper.update(obj);
	}

	public List<CustAbility> findAll() {
		return mapper.findAll();
	}

	public List<CustAbility> findByCustId(Long custId) {
		return extMapper.findByCustId(custId);
	}

	@SuppressWarnings("unused")
	public List<CustAbility> find(CustAbility obj) {
		return mapper.find(obj);
	}

	public void batchInsert(List<CustAbility> list) {
		extMapper.batchInsert(list);
	}

	/**
	 * 查询用户所获取的最大特权
	 * @author zhaoqi01
	 * @param custId
	 * @return
	 */
	public CustAbility findMaxAbilityByCustId(Long custId) {
		return extMapper.findMaxAbilityByCustId(custId);
	}
	
	public void toAddAbility(Long custId) throws Exception {
		int loanCount = custLoanProductService.getSuccessLoanCount(custId);
		int cardCount = custRecommendCardService.countApplyRecord(custId);
		int totalCount = loanCount+cardCount;
		
		for(AbilityEnum abilityEnum : AbilityEnum.values()) {
			if(totalCount >= abilityEnum.getConditionCount()){
				addAbility(abilityEnum.getCode(),custId);
			}
		}
	/*	
		if(totalCount >=AbilityLoanCardCountEnum.Diamond.getCode()){
			addAbility(AbilityEnum.Diamond.getCode(),custId);
		}else if(totalCount >=AbilityLoanCardCountEnum.Platinum.getCode()){
			addAbility(AbilityEnum.Platinum.getCode(),custId);
		}else if(totalCount >=AbilityLoanCardCountEnum.Gold.getCode()){
			addAbility(AbilityEnum.Gold.getCode(),custId);
		}else if(totalCount >=AbilityLoanCardCountEnum.Silver.getCode()){
			addAbility(AbilityEnum.Silver.getCode(),custId);
		}*/
		
	}
	
	public void addAbility(Long privilegeId,Long custId){
		CustAbility custAbility = new CustAbility();
		custAbility.setCustId(custId);
		custAbility.setAbilityId(privilegeId);
		List<CustAbility> list = this.find(custAbility);
		custAbility.setAbilityGettime(new Date());
		Calendar ca = Calendar.getInstance();    
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date expireDate = ca.getTime();
        String expireDateStr = ConcurrentDateUtil.format(expireDate);
        custAbility.setAbilityExpiretime(expireDateStr);
		if(list.size()>0){
			custAbility.setCustAbilityId(list.get(0).getCustAbilityId());
			this.update(custAbility);
		}else{
			this.insert(custAbility);
		}				
	}
}